﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using School.Entities;
using System.Collections;

namespace School.Service.BOOKS
{
    public static class SRV_Book_PaymentOrder
    {
        private static SchoolEntities db = new SchoolEntities();
        public static bool OrderPayInsert(SRV_BOOK_Payment_Order payOrder, out long orderId)
        {
            db.AddToSRV_BOOK_Payment_Order(payOrder);
            try
            {
                db.SaveChanges();
                orderId = payOrder.Order_Id;
                return true;
            }
            catch
            {
                orderId = 0;
                return false;
            }
        }

        public static bool OrderPayItemsInsert(SRV_BOOK_PAYMENT_ITEMS payOrderItems)
        {
            db.AddToSRV_BOOK_PAYMENT_ITEMS(payOrderItems);
            try
            {
                db.SaveChanges();
               return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool OrderDiscountInsert(Hashtable table)
        {
            try
            {
                foreach (DictionaryEntry item in table)
                {
                    db.AddToSRV_Book_PaymentDiscount(new SRV_Book_PaymentDiscount()
                    {
                        OrderID = long.Parse(item.Value.ToString()),
                        DiscountID = long.Parse(item.Key.ToString())
                    });
                }
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IEnumerable<SRV_BOOK_Payment_Order> GetPaymentOrder(long paymntOrderNum, long BookId)
        {
            try
            {
                IEnumerable<SRV_BOOK_Payment_Order> query = (from payOrder in db.SRV_BOOK_Payment_Order
                                                   where payOrder.Payment_Order_Number == paymntOrderNum 
                                                   select payOrder);
                return query;
            }
            catch
            {
                return null;
            }
        }
    }

}
